package com.riche.contractbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.riche.contractbackend.common.BaseResponse;
import com.riche.contractbackend.common.ErrorCode;
import com.riche.contractbackend.common.ResultUtils;
import com.riche.contractbackend.exception.BusinessException;
import com.riche.contractbackend.model.domain.Team;
import com.riche.contractbackend.model.domain.User;
import com.riche.contractbackend.model.dto.TeamQuery;
import com.riche.contractbackend.model.request.TeamAddRequest;
import com.riche.contractbackend.model.request.TeamJoinRequest;
import com.riche.contractbackend.model.request.TeamQuiteRequest;
import com.riche.contractbackend.model.request.TeamUpdateRequest;
import com.riche.contractbackend.model.vo.TeamUserListVo;
import com.riche.contractbackend.model.vo.TeamUserVO;
import com.riche.contractbackend.service.TeamService;
import com.riche.contractbackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName: TeamController
 * @Description: 队伍协议层
 * @Author: Gaoruiqi
 * @Date: 2025-09-10 20:02
 * @Version: 1.0
 **/

@RestController
@RequestMapping("/team")
@Slf4j
public class TeamController {

    @Resource
    private UserService userService;

    @Resource
    private TeamService teamService;

    /**
     * 创建一个新队伍
     * @param teamAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request) {
        if (teamAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Team team = new Team();
        BeanUtils.copyProperties(teamAddRequest, team);
        Long teamId = teamService.addTeam(team, loginUser);
        return ResultUtils.success(teamId, "队伍创建成功");
    }

    /**
     * 解散队伍
     * @param teamId
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(@RequestBody long teamId, HttpServletRequest request) {
        if (teamId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数有误");
        }
        User loginUser = userService.getLoginUser(request);
        boolean delete = teamService.deleteTeam(teamId, loginUser);
        if (!delete) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "删除失败");
        }
        return ResultUtils.success(true);
    }

    /**
     * 修改队伍信息
     * @param teamUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest, HttpServletRequest request) {
        if (teamUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        boolean delete = teamService.updateById(teamUpdateRequest, loginUser);
        if (!delete) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "更新失败");
        }
        return ResultUtils.success(true, "更新成功");
    }

    @GetMapping("/get")
    public BaseResponse<Team> getTeamById(@RequestParam long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = teamService.getById(id);
        if (team == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "获取失败");
        }
        return ResultUtils.success(team);
    }

    /**
     * 展示队伍列表以及关联的用户信息
     * @param teamQuery
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<TeamUserListVo> listTeam(@RequestBody(required = false) TeamQuery teamQuery, HttpServletRequest request) {
        /*if (teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }*/
        User loginUser = userService.getLoginUser(request);
        TeamUserListVo teamList = teamService.listTeams(teamQuery, loginUser);
        return ResultUtils.success(teamList);
    }

    @GetMapping("/list/page")
    public BaseResponse<Page<Team>> listTeamByPage(TeamQuery teamQuery) {
        if (teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = new Team();
        try {
            BeanUtils.copyProperties(team, teamQuery);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        Page<Team> page = new Page<>(teamQuery.getPageNumber(), teamQuery.getPageSize());
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        Page<Team> resultPage = teamService.page(page, queryWrapper);
        return ResultUtils.success(resultPage, "查询成功");
    }

    /**
     * 加入队伍
     * @param teamJoinRequest
     * @param request
     * @return
     */
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinRequest teamJoinRequest, HttpServletRequest request) {
        if (teamJoinRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "请先登录");
        }
        Boolean b = teamService.joinTeam(teamJoinRequest, loginUser);
        if (!b) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "最多加入5个队伍");
        }
        return ResultUtils.success(true, "加入成功");
    }


    /**
     * 退出队伍
     * @param teamQuiteRequest
     * @param request
     * @return
     */
    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeam(@RequestBody TeamQuiteRequest teamQuiteRequest, HttpServletRequest request) {
        if (teamQuiteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数有误");
        }
        User loginUser = userService.getLoginUser(request);
        Boolean result = teamService.quitTeam(teamQuiteRequest, loginUser);
        return ResultUtils.success(result, "退出成功");
    }


    /**
     * 获取我创建的队伍
     * @param teamQuery
     * @param request
     * @return
     */
    @GetMapping("/listMy")
    public BaseResponse<TeamUserListVo> listMyTeams(@RequestBody(required = false) TeamQuery teamQuery, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        boolean isAdmin = userService.isAdmin(loginUser);
        teamQuery.setUserId(loginUser.getId());
        TeamUserListVo teamList = teamService.listTeams(teamQuery, loginUser);
        return ResultUtils.success(teamList);
    }

}
